package com.deng.util
{
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	
	public class DataGridToHtml
	{
		public function DataGridToHtml()
		{
		}

		public function convertDGToHTMLTable(dg:DataGrid):String {
			var font:String = dg.getStyle('fontFamily');
		    var size:String = dg.getStyle('fontSize');
		    var str:String = '';
		    var colors:String = '';
		    var style:String = 'style="font-family:'+font+';font-size:'+size+'pt;"';                
		    var hcolor:Array;
		
		    //设置标题行颜色
		    if(dg.getStyle("headerColor") != undefined) {
		     	hcolor = [dg.getStyle("headerColor")];
		    } else {
				hcolor = dg.getStyle("headerColors");
		    }
		    //根据datagrid设置基础结构
		
		    str+= '<table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'">';
			//设置列头          
		
		    for(var i:int = 0;i<dg.columns.length;i++) {
		        colors = dg.getStyle("themeColor");
		        if(dg.columns[i].headerText != undefined) {
					str+="<th "+style+">"+dg.columns[i].headerText+"</th>";
				} else {
				    str+= "<th "+style+">"+dg.columns[i].dataField+"</th>";
		        }
		    }
			str += "</tr></thead><tbody>";
			colors = dg.getStyle("alternatingRowColors");
		
		    //设置单元格数据，数据遍历DataGird
		
		    for(var j:int =0;j<dg.dataProvider.length;j++) {                 
				str+="<tr width=\""+Math.ceil(dg.width)+"\">";
				for(var k:int=0; k < dg.columns.length; k++) {
					if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) {
						if(dg.columns[k].labelFunction != undefined) {
							var f:Function = dg.columns[k].labelFunction;
							Alert.show(f+"");
							str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+"</td>";
						} else {
							str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]+"</td>";
						}
					}
				}
				str += "</tr>";
		    }
			str+="</tbody></table>";
			return str;
		}
	}
}